﻿using System;
using System.Collections.Generic;
using System.Linq;

namespace FunctionalProgramming.PatternMatching.CollectionMatchingRules
{
   internal class TailAndRest<T> : ICollectionMatchingRule<T>
   {
      private readonly Action<T, IEnumerable<T>> action;

      public TailAndRest(Action<T, IEnumerable<T>> action)
      {
         this.action = action;
      }

      public bool DoesCollectionMatch(IEnumerable<T> collection)
      {
         bool result = collection.Count() > 0;
         return result;
      }

      public void ExecuteWithCollection(IEnumerable<T> collection)
      {
         T tail = collection.Last();
         IEnumerable<T> rest = collection.Take(collection.Count() - 1);
         action(tail, rest);
      }
   }
}